package com.nami.gchat.infra.middleware.mq;

import com.nami.gate.external.basic.constants.ConverTopicConst;
import com.nami.gate.external.basic.constants.DispatcherTopicConst;
import com.nami.gate.external.basic.constants.StoreTopicConst;
import com.nami.gate.external.basic.event.EventPublisher;
import com.nami.gate.external.basic.event.model.CommonEvent;
import com.nami.transport.proto.codec.MqTransMsg;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class GrpEventPublisher implements EventPublisher {

    private final RocketMQTemplate rocketMQTemplate;

    @Override
    public void publish(CommonEvent event) {
        try {
            switch (event.getEventType()) {
                case DISPATCH_GRP_MSG:
                    MqTransMsg.GrpLogic2DispatchBody msgDomain = (MqTransMsg.GrpLogic2DispatchBody ) event.getPayload();
                    String targetId = event.getExtra();
                    rocketMQTemplate.syncSendOrderly(DispatcherTopicConst.GCHAT_TOPIC, msgDomain.toByteArray(), targetId);
                    break;
                case UPD_GRP_CONVERS:
                    MqTransMsg.GrpLogic2DispatchBody grpMsgDomain = (MqTransMsg.GrpLogic2DispatchBody) event.getPayload();
                    String targetId2 = event.getExtra();
                    rocketMQTemplate.syncSendOrderly(ConverTopicConst.GCHAT_UPD_CONVERS_TOPIC, grpMsgDomain.toByteArray(), targetId2);
                    break;
                case STORE_GRP_MSG:
                    MqTransMsg.GrpLogic2DispatchBody store = (MqTransMsg.GrpLogic2DispatchBody)event.getPayload();
                    rocketMQTemplate.syncSend(StoreTopicConst.GCHAT_STORE_MSG_TOPIC, store.toByteArray());
                    break;
                default:
                    log.error("[c2g] 群聊消费者未匹配类型：{}", event.getEventType());
                    break;
            }
        } catch (Exception var2) {
            var2.printStackTrace();
            log.error("[c2g-publisher] 群聊消费者出现问题：", var2);
        }
    }

}
